package com.anyou.mall.index.controller;

import com.anyou.common.core.controller.BaseController;
import com.anyou.common.core.domain.ApiResult;

import com.anyou.common.core.page.TableDataInfo;
import com.anyou.common.utils.DateUtils;
import com.anyou.common.vo.StartEnd;
import com.anyou.mall.index.param.*;
import com.anyou.mall.index.vo.*;
import com.anyou.mall.order.service.IOrdOrderService;
import com.anyou.mall.partner.domain.PartnerLavel;
import com.anyou.mall.use.domain.UserSystemNotice;
import com.anyou.mall.use.service.IUserService;
import com.anyou.mall.use.service.IUserSystemNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 管理员首页Controller
 *
 * @author gaochaohao
 * @date 2022-04-02
 */
@Api(tags = "管理员首页")
@RestController
@RequestMapping("/admin/index")
public class AdminIndexController extends BaseController
{

    @Autowired
    private IOrdOrderService ordOrderService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserSystemNoticeService systemNoticeService;

    /**
     * 8个卡片数据
     */
    @ApiOperation("7个卡片数据")
    @PreAuthorize("@ss.hasPermi('admin:index:card8')")
    @PostMapping("/card8")
    public ApiResult<Card8Vo> card8() {
        return ordOrderService.card8Num();
    }

    /**
     * 订单数量折线走势图
     */
    @ApiOperation("订单数量折线走势图(商家首页或后台首页)")
    @PreAuthorize("@ss.hasPermi('admin:index:orderNum')")
    @PostMapping("/orderNum")
    public ApiResult<List<OrderNumTjVo>> orderNum(@Valid @RequestBody OrderNumParam param) {
        if(param.getType()!=null){
            StartEnd se = DateUtils.getStartEndByType(param.getType());
            param.setStartTime(se.getStartTime());
            param.setEndTime(se.getEndTime());
        }
        return ordOrderService.orderNumTj(param);
    }

    /**
     * 平台收入折线走势图
     */
    @ApiOperation("平台收入折线走势图")
    @PreAuthorize("@ss.hasPermi('admin:index:platMoney')")
    @PostMapping("/platMoney")
    public ApiResult<List<OrderNumTjVo>> platMoney(@Valid @RequestBody OrderNumParam param) {
        if(param.getType()!=null){
            StartEnd se = DateUtils.getStartEndByType(param.getType());
            param.setStartTime(se.getStartTime());
            param.setEndTime(se.getEndTime());
        }
        List<OrderNumTjVo> list=ordOrderService.getPlatMoney(param);
        return ApiResult.success(list);
    }

    /**
     * 新增用户折线走势图
     */
    @ApiOperation("新增用户折线走势图")
    @PreAuthorize("@ss.hasPermi('admin:index:userData')")
    @PostMapping("/userData")
    public ApiResult<List<UserNum>> userData(@Valid @RequestBody UserNumParam param) {
        if(param.getType()!=null){
            StartEnd se = DateUtils.getStartEndByType(param.getType());
            param.setStartTime(se.getStartTime());
            param.setEndTime(se.getEndTime());
        }
        List<UserNum> list=userService.getUserNumByDate(param);
        return ApiResult.success(list);
    }
    /**
     * 消息列表
     */
    @ApiOperation("消息列表")
    @PreAuthorize("@ss.hasPermi('admin:index:userMsgList')")
    @PostMapping("/userMsgList")
    public TableDataInfo<List<UserSystemNotice>> userMsgList(@Valid UserMsgParam param) {
        startPage();
        List<UserSystemNotice> list=systemNoticeService.getMsgList(param);
        return getDataTable(list);
    }
    /**
     * 消息数量
     */
    @ApiOperation("消息数量")
    @PreAuthorize("@ss.hasPermi('admin:index:msgNum')")
    @PostMapping("/msgNum")
    public ApiResult<MsgNum> userReadMsg(@Valid UserMsgParam2 param) {
        UserMsgParam userMsgParam = new UserMsgParam();
        BeanUtils.copyProperties(param, userMsgParam);
        MsgNum msgNum= getMsgNum(userMsgParam);

        return ApiResult.success(msgNum);
    }

    /**
     * 计算消息数量
     * 消息类型：1-支付订单，2-售后订单，3-系统
     * @param userMsgParam
     * @return
     */
    private MsgNum getMsgNum( UserMsgParam userMsgParam) {
        MsgNum msgNum = new MsgNum();
        //总消息数量
        Integer totalNum=systemNoticeService.countMsgList(userMsgParam);
        //新订单消息数量
        userMsgParam.setNoticeType(1);
        Integer orderNum=systemNoticeService.countMsgList(userMsgParam);
        //售后订单消息数量
        userMsgParam.setNoticeType(2);
        Integer serveNum=systemNoticeService.countMsgList(userMsgParam);
        //售后系统消息数量
        userMsgParam.setNoticeType(3);
        Integer sysNum=systemNoticeService.countMsgList(userMsgParam);
        msgNum.setTotalNum(totalNum);
        msgNum.setOrderNum(orderNum);
        msgNum.setServeNum(serveNum);
        msgNum.setSysNum(sysNum);
        return msgNum;
    }

    /**
     * 消息已读
     */
    @ApiOperation("消息已读")
    @PreAuthorize("@ss.hasPermi('admin:index:userReadMsg')")
    @PostMapping("/userReadMsg")
    public ApiResult userReadMsg(@Valid MsgParam param) {
        systemNoticeService.userReadMsg(param);
        return ApiResult.success();
    }

//    /**
//     * 订单分析饼图
//     * 还应该有一种订单走物流的
//     */
//    @ApiOperation("订单分析饼图")
//    @PreAuthorize("@ss.hasPermi('admin:index:orderAnalysis')")
//    @PostMapping("/orderAnalysis")
//    public ApiResult<OrderAnalysis> orderAnalysis(@Valid @RequestBody UserNumParam userNumParam) {
//        OrderAnalysis orderAnalysis=userService.getOrderAnalysis(userNumParam);
//        return ApiResult.success(orderAnalysis);
//    }
    /**
     * 订单类型分析饼图
     *
     */
    @ApiOperation("订单类型分析饼图")
    @PreAuthorize("@ss.hasPermi('admin:index:orderTypeAnalysis')")
    @PostMapping("/orderTypeAnalysis")
    public ApiResult<OrderTypeAnaVo> orderTypeAnalysis(@Valid @RequestBody UserNumParam param) {
        if(param.getType()!=null){
            StartEnd se = DateUtils.getStartEndByType(param.getType());
            param.setStartTime(se.getStartTime());
            param.setEndTime(se.getEndTime());
        }
        OrderTypeAnaVo otaVo=userService.getOrderTypeAna(param);
        return ApiResult.success(otaVo);
    }
    /**
     * 订单分析环形图
     *
     */
    @ApiOperation("订单分析环形图")
    @PreAuthorize("@ss.hasPermi('admin:index:orderToroidal')")
    @PostMapping("/orderToroidal")
    public ApiResult<OrderTypeAnaVo2> orderToroidal(@Valid @RequestBody UserNumParam param) {
        if(param.getType()!=null){
            StartEnd se = DateUtils.getStartEndByType(param.getType());
            param.setStartTime(se.getStartTime());
            param.setEndTime(se.getEndTime());
        }
        OrderTypeAnaVo2 otaVo=userService.getOrderToroidal(param);
        return ApiResult.success(otaVo);
    }
    /**
     * 订单分析走势图
     * 订单数量走势图
     */
    @ApiOperation("订单分析走势图")
    @PreAuthorize("@ss.hasPermi('admin:index:orderAnalysis2')")
    @PostMapping("/orderAnalysis2")
    public ApiResult<List<OrderNumTjVo2>> orderAnalysis2(@Valid @RequestBody OrderNumParam param) {
        if(param.getType()!=null){
            StartEnd se = DateUtils.getStartEndByType(param.getType());
            param.setStartTime(se.getStartTime());
            param.setEndTime(se.getEndTime());
        }
        List<OrderNumTjVo2> list=ordOrderService.getPlatMoney2(param);
        return ApiResult.success(list);
    }
    /**
     * 购水用途饼状图
     */
    @ApiOperation("购水用途饼状图")
    @PreAuthorize("@ss.hasPermi('admin:index:waterUse')")
    @PostMapping("/waterUse")
    public ApiResult<OrderTypeAnaVo2> waterUse(@Valid @RequestBody OrderNumParam param) {
        if(param.getType()!=null){
            StartEnd se = DateUtils.getStartEndByType(param.getType());
            param.setStartTime(se.getStartTime());
            param.setEndTime(se.getEndTime());
        }
        OrderTypeAnaVo2 otaVo=userService.waterUse(param);
        return ApiResult.success(otaVo);

    }

}
